/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author GDF
 */
public class manipular {
    conexion db = conexion.getInstance();
   
    
    public static ResultSet correrConsulta(String query){
        ResultSet result = null;
        try {
             Statement cStatement = conexion.con.createStatement();
             result = cStatement.executeQuery(query);
             return result;
        } catch (SQLException ex) {
            System.out.println("Error al realizar la consulta");
        }
        return result;
    }

    public void runUpdate(String sentence){
	try {
            Statement stm = (Statement) conexion.con.createStatement();
            stm.executeUpdate(sentence);
        }
        catch  (Exception ex){
            ex.printStackTrace();
        }	
    }
    
    public PreparedStatement newRecord(String table, String[] columns){
        PreparedStatement stmt = null;
        try {
            String values = "";
            String columnNames = "";
            int i = 0;
            while (i != columns.length){
                values += "?,";
                columnNames += (columnNames.equals(""))? columns[i] : ", " + columns[i];
                i++;
            }
            values = (String) values.subSequence(0, values.length()-1);

            String command = "INSERT INTO " + table + " (" + columnNames + ") VALUES (" + values +")";
            stmt = (PreparedStatement) db.con.prepareStatement(command);
        }
        catch (Exception ex){
            System.err.println("ERROR createInsertStatement");
            ex.printStackTrace();
        }
        return stmt;
    }
    
}
